﻿$(document).ready(function () {
    $.fn.calculator = function (method, args) {
        var jq = this;
        if (typeof (method) != "string") {
            args = method;
            method = "init";
        }
        var fn = methods[method];
        if ($.isFunction(fn)) {
            return fn(jq, args);
        }
    }
    $.fn.calculator.defaultOptions = {
        "modal": true
    };
    function draw(jq) {
        var options = methods.options(jq);
        jq.addClass("dscalculator");
        jq.window({
            "width": 240,
            "height": 230,
            "title": "计算器",
            "maximizable": false,
            "minimizable": false,
            "collapsible": false,
            "modal": options.modal,
            "onClose": function () {
                jq.window("destroy");
            }
        });
        var inputDiv = $("<div class='inputBar'/>").appendTo(jq);
        var buttonDiv = $("<div class='buttonBar'/>").appendTo(jq);

        var box = $("<div class='box'/>").appendTo(inputDiv); //
        var input = $("<input class='displayInput' readonly='readonly' type='text' value='0' />").appendTo(box);
        //alert(box.innerWidth() + "," + box.width());
        input.width(box.width() - 2);
        var buttonLayout = [
           ["7", "8", "9", "+"],
           ["4", "5", "6", "-"],
           ["1", "2", "3", "×"],
           ["0", ".", "C", "÷"],
           ["←", "=", "确定"]
        ];
        var table = $("<table border=\"0\" collapse=\"0\" style='width:100%;border-collapse:separate;border-spacing:3px 3px'/>").appendTo(buttonDiv);
        for (var i = 0; i < buttonLayout.length; i++) {
            var row = buttonLayout[i];
            var tr = $("<tr/>").appendTo(table);
            for (var n = 0; n < row.length; n++) {
                var button = row[n];
                var td = $("<td align='left'/>").appendTo(tr);
                if (button) {
                    var a = $("<A/>").appendTo(td);
                    a.linkbutton({
                        "text": button
                    });
                    a.bind("click", function (evt) {
                        var b = $(evt.currentTarget);
                        doInput(jq, b);
                    })
                    if (button == "确定") {
                        td.attr({ "colspan": 2 });
                        //a.find(".l-btn-text").width(75).css("textAlign", "center");
                    }
                    else {
                        a.find(".l-btn-text").width(15).css("textAlign", "center");
                    }
                }
            }
        }
    }

    function doInput(jq, b) {
        var options = methods.options(jq);
        var oInput = jq.find("INPUT.displayInput");
        var sOpr = jq.data("opr");
        var sHtml = $.trim(b.text()); //this.innerHTML.replace(' ', '');
        var sNum1 = null;
        switch (sHtml) {
            case '=':
                sNum1 = jq.data("fnum");
                var val = calc(parseFloat(sNum1), parseFloat(oInput.val()), sOpr);
                oInput.val(val);
                jq.data("fnum", "");
                jq.data("opr", "");
                jq.data("nc", true);
                break;
            case '+':
            case '-':
            case '×':
            case '÷':
                jq.data("nc", true);
                if (jq.data("fnum").length != 0) {
                    sNum1 = jq.data("fnum");
                    if (sOpr) {
                        var val = calc(parseFloat(sNum1), parseFloat(oInput.val()), sOpr);
                        oInput.val(val);
                    }
                }
                jq.data("fnum", oInput.val());
                jq.data("opr", sHtml);
                break;
            case 'C':
                oInput.val("0");
                jq.data("nc", true)
                jq.data("opr", "")
                jq.data("reset", true)
                jq.data("fnum", "0");
                break;
            case "确定":
                if (options.onComplate) {
                    if (jq.data("fnum").length != 0) {
                        sNum1 = jq.data("fnum");
                        if (sOpr) {
                            var val = calc(parseFloat(sNum1), parseFloat(oInput.val()), sOpr);
                            oInput.val(val);
                        }
                    }
                    options.onComplate(oInput.val());
                }
                jq.window("close");
                break;
            default: //数字或小数点或者是回退符
                if (sHtml == "←") {
                    var val = oInput.val();
                    if (val.length > 0) {
                        var v = val.substr(0, val.length - 1);
                        oInput.val(v);
                    }
                }
                else {
                    if (jq.data("nc")) {
                        var val = oInput.val("");
                    }
                    var str = sHtml
                    if (sHtml == ".") {
                        if (oInput.val().indexOf(".") >= 0) {
                            str = "";
                        }
                    }
                    oInput.val(oInput.val() + str);
                }
                jq.data("nc", false);
                break;
        }
    }

    function calc(iNum1, iNum2, sOpr) {
        var iResult = 0;
        switch (sOpr) {
            case '×':
                iResult = iNum1 * iNum2;
                break;
            case '+':
                iResult = iNum1 + iNum2;
                break;
            case '-':
                iResult = iNum1 - iNum2;
                break;
            case '÷':
                iResult = iNum1 / iNum2;
                break;
            default:
                iResult = iNum2;
        }

        return iResult;
    }


    var methods = $.fn.calculator.methods = {
        "init": function (jq, args) {
            jq.each(function (i, el) {
                var eljq = $(el);
                var options = $.extend({}, $.fn.calculator.defaultOptions, args);
                eljq.data("options", options);
                eljq.data("counter", true);
                eljq.data("nc", true)//表示是否要清空文本框
                eljq.data("opr", "")//表示运算符
                eljq.data("reset", true)//计算初始化
                eljq.data("fnum", "0"); //第一个数字
                draw(eljq);
            });
        },
        "options": function (jq, args) {
            return jq.data("options");
        }
    }
});